package live.zs.mybatisplusboot;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.BiConsumer;

/**
 * @author madison
 * @description
 * @date 2020/7/8 1:59 下午
 */
public class CodeGenerator {

    @Test
    public void codeGenerator() {

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder(
                "jdbc:mysql://localhost:3306/data_integration?useUnicode=true&useSSL=false&characterEncoding=UTF-8",
                "madison",
                "zs123456").build();

        // 全局配置
        String projectPath = System.getProperty("user.dir");
        String srcPath = "/src/main/java";
        System.out.println("===============文件产出目录: " + projectPath + srcPath);
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .outputDir(projectPath + srcPath)
                .author("madison")
                .disableOpenDir()
                .commentDate("yyyy-MM-dd HH:mm:ss")
                .fileOverride()
                .dateType(DateType.TIME_PACK)
                // .enableSwagger()
                .build();

        // 自定义配置
        BiConsumer<TableInfo, Map<String, Object>> biConsumer = (tableInfo, mapInfo) -> {
            System.out.printf("自定义配置BiConsumer tableInfo:%s%n", tableInfo.toString());
            tableInfo.setComment("自定义配置BiConsumer tableInfo comment");
            mapInfo.forEach((key, value) -> {
                System.out.printf("自定义配置BiConsumer mapInfo: key: %s--------- value: %s%n ", key, value);
            });
        };
        InjectionConfig injectionConfig = new InjectionConfig.Builder().beforeOutputFile(biConsumer).build();

        // 包配置
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent("live.zs.mybatisplusboot")
//                .moduleName("")
//                .controller("controller")
//                .entity("entity")
//                .service("service")
//                .mapper("mapper")
//                .xml("mapper.xml")
//                .serviceImpl("service.impl")
                .build();

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig.Builder().build();
        // 配置自定义输出模板
        // 指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();

        // 策略配置
        StrategyConfig strategy = new StrategyConfig.Builder()
//                .addTablePrefix(packageConfig.getModuleName() + "_")
                .addInclude("tb_user")
//                .addInclude("tb_order")
                .enableCapitalMode()
                .entityBuilder()
                .naming(NamingStrategy.underline_to_camel)
                .build();


        // 如果模板引擎是 freemarker
        // String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";
        FreemarkerTemplateEngine freemarkerTemplateEngine = new FreemarkerTemplateEngine();
        freemarkerTemplateEngine.templateFilePath("");


        // 代码生成器
        AutoGenerator generator = new AutoGenerator(dataSourceConfig);
        generator.global(globalConfig);
        generator.injection(injectionConfig);
        generator.packageInfo(packageConfig);
        generator.template(templateConfig);
        generator.strategy(strategy);
//        generator.execute(freemarkerTemplateEngine);
        generator.execute();

//        new SimpleAutoGenerator() {
//            @Override
//            public IConfigBuilder<DataSourceConfig> dataSourceConfigBuilder() {
//                return new DataSourceConfig.Builder(
//                        "jdbc:mysql://localhost:3306/data_integration?useUnicode=true&useSSL=false&characterEncoding=UTF-8",
//                        "madison",
//                        "zs123456");
//            }
//        }.execute();
    }
}
